package org.jeecg.modules.basedata.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.entity.basedata.BaseFloor;
import org.jeecg.entity.basedata.BaseFloorInfo;
import org.jeecg.modules.basedata.mapper.BaseFloorInfoMapper;
import org.jeecg.modules.basedata.mapper.BaseFloorMapper;
import org.jeecg.modules.basedata.service.IBaseFloorService;
import org.jeecg.vo.basedata.BaseFloorPage;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 楼宇
 * @Author: jeecg-boot
 * @Date:   2019-07-02
 * @Version: V1.0
 */
@Service
@Slf4j
public class BaseFloorServiceImpl extends ServiceImpl<BaseFloorMapper, BaseFloor> implements IBaseFloorService {

	@Autowired
	private BaseFloorMapper baseFloorMapper;
	@Autowired
	private BaseFloorInfoMapper baseFloorInfoMapper;

	@Override
	public Page<BaseFloorPage> getBaseFloorList(Page<BaseFloorPage> page, Wrapper<BaseFloorPage> wrapper) {
		//log.info("======================"+wrapper.getCustomSqlSegment());
		//log.info("======================"+wrapper.getSqlSegment());
		return page.setRecords(baseFloorMapper.getBaseFloorPage(page, wrapper));
	}

	@Override
	public BaseFloorPage selectByMainId(String mainId) {
		return baseFloorMapper.selectByMainId(mainId);
	}
	
	@Override
	@Transactional
	public void saveMain(BaseFloor baseFloor, BaseFloorInfo baseFloorInfo) {
		baseFloorMapper.insert(baseFloor);
		baseFloorInfo.setId(baseFloor.getId());
		baseFloorInfoMapper.insert(baseFloorInfo);
	}

	@Override
	@Transactional
	public void updateMain(BaseFloor baseFloor,BaseFloorInfo baseFloorInfo) {
		baseFloorMapper.updateById(baseFloor);

		//1.先删除子表数据
		//baseFloorInfoMapper.deleteByMainId(baseFloor.getId());

		//2.子表数据重新插入
		baseFloorInfo.setId(baseFloor.getId());
		baseFloorInfoMapper.updateById(baseFloorInfo);
	}

	@Override
	@Transactional
	public void delMain(String id) {
		//baseFloorInfoMapper.deleteByMainId(id);
		baseFloorMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			//baseFloorInfoMapper.deleteByMainId(id.toString());
			baseFloorMapper.deleteById(id);
		}
	}

	@Override
	public Page<BaseFloorPage> listByParams(Page<BaseFloorPage> page,BaseFloorPage baseFloor) {
		return page.setRecords(baseFloorMapper.listByParams(page,baseFloor));
	}

	@Override
	public Integer getRoomCount(String id) {
		return baseFloorMapper.getRoomCount(id);
	}

	@Override
	public Integer getRoomCounts(String[] ids) {
		return baseFloorMapper.getRoomCounts(ids);
	}

	@Override
	public BaseFloor queryFloorName(String projectId, String floorName) {
		return baseFloorMapper.queryFloorName(projectId,floorName);
	}

	@Override
	public String queryProjectName(String projectId) {
		return baseFloorMapper.queryProjectName(projectId);
	}

	@Override
	public List<BaseFloor> queryFloorList(String projectId) {
 		List<BaseFloor> list = baseFloorMapper.queryFloorList(projectId);
		return list;
	}

	@Override
	public BaseFloor getByProjectIdName(String projectId, String name){
		return  baseFloorMapper.getByProjectIdName(projectId, name);
	}

}
